﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZdSoft.Core;
using ZdSoft.BLL;
using ZdSoft.IDAL;
using ZdSoft.Model;
using ZdSoft.Web.OnlineStudy.Apps;
using System.Threading.Tasks;

namespace ZdSoft.Web.OnlineStudy.Controllers
{
    public class ResourceController : BaseController
    {
        // GET: Resource
        public ActionResult Index()
        {
            IList<Resource> listR = Container.Instance.Resolve<IResourceDAL>().GetAll();
            ViewBag.resourceCount = listR.Count;
            var query = listR.Skip(0).Take(10).ToList();
            return View(query);
        }
        [HttpPost]
        public JsonResult Index(int pageSize, int pageIndex)
        {
            IList<Resource> listR = Container.Instance.Resolve<IResourceDAL>().GetAll();
            var query = listR.Skip((pageIndex-1)*pageSize).Take(pageSize).ToList();
            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            string quR =js.Serialize(query);
            return Json(quR);
        }
        public ViewResult Create()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Create(Resource rs)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    HttpPostedFileBase file = Request.Files.Get("ResourcesUrl") as HttpPostedFileBase;
                    if (file != null)
                    {
                        string filePath = Path.Combine(HttpContext.Server.MapPath("~/Uploads"), Path.GetFileName(file.FileName));
                        if (!Directory.Exists(HttpContext.Server.MapPath("~/Uploads")))
                        {
                            Directory.CreateDirectory(HttpContext.Server.MapPath("~/Uploads"));
                        }
                        if (!System.IO.File.Exists(filePath))
                        {
                            file.SaveAs(filePath);
                        }
                        try
                        {
                            rs.ResourcesUrl = filePath;//重新创建路径值
                            Container.Instance.Resolve<IResourceDAL>().Create(rs);
                            ModelState.Clear();//创建成功后清空数据
                        }
                        catch
                        {
                            return Json(new { status = "nosuccess" });
                        }
                        return Json(new { status = "success" });
                    }
                    else
                    {
                        return Json(new { status = "nosuccess" });
                    }
                }
                else
                {
                    return Json(new { status = "nosuccess" });
                }
            }
            catch
            {
                return Json(new { status = "nosuccess" });
            }
        }
        public ActionResult Delete(int resourceID)
        {
            try
            {
                Container.Instance.Resolve<IResourceDAL>().Delete(resourceID);
                return Redirect("http://localhost:24833/Resource");
            }
            catch
            {
                return Redirect("http://localhost:24833/Resource");
            }
        }
    }
}